package com.effective.java.inheritence;

import java.util.Collection;
import java.util.HashSet;

/**
 * This class adds capabilities to count all the elements that were added to a
 * set.
 * 
 * @author andreas.landerer
 * 
 * @param <E>
 *            any type
 */
public class ProperHashSetComposition<E> extends ForwardingSet<E> {

	/**
	 * the variable counts how many objects were added to the set.
	 */
	private int addCount = 0;

	public ProperHashSetComposition() {

		super(new HashSet<E>());
	}

	@Override
	public boolean add(E arg0) {

		this.addCount++;
		return super.add(arg0);
	}

	@Override
	public boolean addAll(Collection<? extends E> subset) {

		this.addCount += subset.size();
		return super.addAll(subset);
	}

	/**
	 * Get the value how many times an element was added to this set
	 * 
	 * @return the value how many times an element was added to this set
	 */
	public int getAddCount() {

		return this.addCount;
	}
}
